﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using DrugsShopManagement.DBCLASS;

namespace DrugsShopManagement
{
    public partial class FrmDrugsBuySearch : DevExpress.XtraEditors.XtraForm
    {
        public FrmDrugsBuySearch()
        {
            InitializeComponent();
            //  u_name = user_name;
        }
        Operator op = new Operator();
        public string u_name;

        private void FrmDrugsBuySearch_Load(object sender, EventArgs e)
        {
            //this.dateTimePicker1.CustomFormat = "yyyy-MM-dd HH:mm:ss";
            //this.dateTimePicker2.CustomFormat = "yyyy-MM-dd HH:mm:ss";
            this.tabControl1.SelectedTab = this.tabPage1;
            //窗体最大化
            this.WindowState = FormWindowState.Maximized;

            try
            {
                DataTable dt = new DataTable();
                string sql = "SELECT [drugsBuyId] ID,[drugsBuyCode] 采购单号,[drugsId] 药品ID,[drugsName] 药品名称,[drugsPY] 拼音码,[drugsSpecification] 药品规格,[drugsUnit]计量单位 ,[drugsRegNum]批准文号,[drugsBuyNum]采购数量 ,[drugsBuyPrice]采购单价 ,[drugsSalePrice]销售价格,[drugsBuyFactory]生产厂家,[drugsBuyProvider]供应商 ,[drugsBuyStoreRoom]库房 ,[drugsBuyBatch]批号,[drugsBuyTotalPrice]总价 ,[drugsBuyerName]采购人员 ,[drugsBuyDate]采购日期 from drugsBuy order by drugsId";
                dt = op.getDataTable(sql, "drugsBuy");
                dataGridView1.DataSource = dt;

            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }


        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSearch_Click(sender, e);
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dr = new DataTable();
                dr = op.getDataTable("select * from drugsBuy where drugsId='" + tbdrugsID.Text + "'", "drugsBuy");
                this.tbdrugsID.Text = dr.Rows[0]["drugsId"].ToString();
                this.tbdrugsName.Text = dr.Rows[0]["drugsName"].ToString();
                this.tbdrugsPY.Text = dr.Rows[0]["drugsPY"].ToString();
                this.tbdrugsSpecification.Text = dr.Rows[0]["drugsSpecification"].ToString();
                this.tbdrugsUnit.Text = dr.Rows[0]["drugsUnit"].ToString();
                this.tbdrugsInNum.Text = dr.Rows[0]["drugsBuyNum"].ToString();
                this.tbdrugsBuyPrice.Text = dr.Rows[0]["drugsBuyPrice"].ToString();
                this.tbdrugsBuyFactory.Text = dr.Rows[0]["drugsBuyFactory"].ToString();
                this.tbdrugsSalePrice.Text = dr.Rows[0]["drugsSalePrice"].ToString();

                this.tbdrugsBuyDate.Text = dr.Rows[0]["drugsBuyDate"].ToString();
                //生成销售单号
                GetDrugsInCode();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }

                 
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DataTable dt_Search = new DataTable();
                dt_Search = op.getDataTable("SELECT [drugsBuyId] ID,[drugsBuyCode] 采购单号,[drugsId] 药品ID,[drugsName] 药品名称,[drugsPY] 拼音码,[drugsSpecification] 药品规格,[drugsUnit]计量单位 ,[drugsRegNum]批准文号,[drugsBuyNum]采购数量 ,[drugsBuyPrice]采购单价 ,[drugsSalePrice]销售价格,[drugsBuyFactory]生产厂家,[drugsBuyProvider]供应商 ,[drugsBuyStoreRoom]库房 ,[drugsBuyBatch]批号,[drugsBuyTotalPrice]总价 ,[drugsBuyerName]采购人员 ,[drugsBuyDate]采购日期 FROM [Drugs].[dbo].[drugsBuy]  where drugsBuyId='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'ORDER BY ID", "drugsBuy");
                this.tbdrugsID.Text = dt_Search.Rows[0]["药品ID"].ToString();
                this.tbdrugsName.Text = dt_Search.Rows[0]["药品名称"].ToString();
                this.tbdrugsPY.Text = dt_Search.Rows[0]["拼音码"].ToString();
                this.tbdrugsSpecification.Text = dt_Search.Rows[0]["药品规格"].ToString();
                this.tbdrugsUnit.Text = dt_Search.Rows[0]["计量单位"].ToString();
                this.tbdrugsRegNum.Text = dt_Search.Rows[0]["批准文号"].ToString();
                this.tbdrugsBuyPrice.Text = dt_Search.Rows[0]["采购单价"].ToString();
                this.tbdrugsSalePrice.Text = dt_Search.Rows[0]["销售价格"].ToString();
                this.tbdrugsInTotalPrice.Text = dt_Search.Rows[0]["总价"].ToString();
                this.tbdrugsBuyFactory.Text = dt_Search.Rows[0]["生产厂家"].ToString();
                this.tbdrugsInNum.Text = dt_Search.Rows[0]["采购数量"].ToString();
                this.tbdrugsBatch.Text = dt_Search.Rows[0]["批号"].ToString();
                this.txtBoxDrugsProvider.Text = dt_Search.Rows[0]["供应商"].ToString();
                this.txtBoxDrugsBuyCode.Text = dt_Search.Rows[0]["采购单号"].ToString();
                this.tbdrugsBuyDate.Text = dt_Search.Rows[0]["采购日期"].ToString();
                this.textBox4.Text = dt_Search.Rows[0]["ID"].ToString();
                //生成入库单号
                GetDrugsInCode();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }
          private void GetDrugsInCode()
        {
            try
            {
           
                this.txtBoxDrugsInNum.Text = this.txtBoxKPYID.Text + this.dateTimePicker3.Text + txtBoxDrugsBuyCode.Text;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

          private void button5_Click(object sender, EventArgs e)
          {
              this.Close();
          }

          private void textBox1_TextChanged(object sender, EventArgs e)
          {
              DataTable dt1 = new DataTable();
              string sql1 = "SELECT [drugsBuyId] ID,[drugsBuyCode] 采购单号,[drugsId] 药品ID,[drugsName] 药品名称,[drugsPY] 拼音码,[drugsSpecification] 药品规格,[drugsUnit]计量单位 ,[drugsRegNum]批准文号,[drugsBuyNum]采购数量 ,[drugsBuyPrice]采购单价 ,[drugsSalePrice]销售价格,[drugsBuyFactory]生产厂家,[drugsBuyProvider]供应商 ,[drugsBuyStoreRoom]库房 ,[drugsBuyBatch]批号,[drugsBuyTotalPrice]总价 ,[drugsBuyerName]采购人员 ,[drugsBuyDate]采购日期 FROM [Drugs].[dbo].[drugsBuy] where drugsName like '%" + this.textBox1.Text.Trim() + "%'or drugsId like '%" + this.textBox1.Text.ToUpper() + "%' order by drugsId";
              dt1 = op.getDataTable(sql1, "drugsBuy");
              dataGridView1.DataSource = dt1;
          }

          private void textBox2_TextChanged(object sender, EventArgs e)
          {
              try
              {
                  DataTable dt = new DataTable();
                  string sql = "SELECT [drugsBuyId] ID,[drugsBuyCode] 采购单号,[drugsId] 药品ID,[drugsName] 药品名称,[drugsPY] 拼音码,[drugsSpecification] 药品规格,[drugsUnit]计量单位 ,[drugsRegNum]批准文号,[drugsBuyNum]采购数量 ,[drugsBuyPrice]采购单价 ,[drugsSalePrice]销售价格,[drugsBuyFactory]生产厂家,[drugsBuyProvider]供应商 ,[drugsBuyStoreRoom]库房 ,[drugsBuyBatch]批号,[drugsBuyTotalPrice]总价 ,[drugsBuyerName]采购人员 ,[drugsBuyDate]采购日期 FROM [Drugs].[dbo].[drugsBuy] where drugsName like '%" + this.textBox1.Text.Trim() + "%'or drugsPY like '%" + this.textBox1.Text.ToUpper() + "%' order by drugsId";
                  dt = op.getDataTable(sql, "drugsBuy");
                  dataGridView1.DataSource = dt;
              }
              catch (Exception ex) { MessageBox.Show(ex.Message); return; }
          }

          private void textBox3_TextChanged(object sender, EventArgs e)
          {
              try
              {
                  DataTable dt = new DataTable();
                  string sql = "SELECT [drugsBuyId] ID,[drugsBuyCode] 采购单号,[drugsId] 药品ID,[drugsName] 药品名称,[drugsPY] 拼音码,[drugsSpecification] 药品规格,[drugsUnit]计量单位 ,[drugsRegNum]批准文号,[drugsBuyNum]采购数量 ,[drugsBuyPrice]采购单价 ,[drugsSalePrice]销售价格,[drugsBuyFactory]生产厂家,[drugsBuyProvider]供应商 ,[drugsBuyStoreRoom]库房 ,[drugsBuyBatch]批号,[drugsBuyTotalPrice]总价 ,[drugsBuyerName]采购人员 ,[drugsBuyDate]采购日期 FROM [Drugs].[dbo].[drugsBuy] where drugsName like '%" + this.textBox1.Text.Trim() + "%'or drugsBuyProvider like '%" + this.textBox1.Text.ToUpper() + "%' order by drugsId";
                  dt = op.getDataTable(sql, "drugsBuy");
                  dataGridView1.DataSource = dt;
              }
              catch (Exception ex) { MessageBox.Show(ex.Message); return; }
          }

          private void button4_Click(object sender, EventArgs e)
          {
              try
            {
                if (this.textBox4.Text == "")
                {
                    MessageBox.Show("请选择需要删除的内容！");
                    return;
                }
                if (MessageBox.Show("确定需要删除药品[ " + this.tbdrugsName.Text.Trim() + " ]吗？", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    op.ExecuteSql("delete from drugsBuy where drugsBuyId='" + textBox4.Text.Trim() + "'");
                    DataTable dt2 = new DataTable();
                    string sql2 = "SELECT [drugsBuyId] ID,[drugsBuyCode] 采购单号,[drugsId] 药品ID,[drugsName] 药品名称,[drugsPY] 拼音码,[drugsSpecification] 药品规格,[drugsUnit]计量单位 ,[drugsRegNum]批准文号,[drugsBuyNum]采购数量 ,[drugsBuyPrice]采购单价 ,[drugsSalePrice]销售价格,[drugsBuyFactory]生产厂家,[drugsBuyProvider]供应商 ,[drugsBuyStoreRoom]库房 ,[drugsBuyBatch]批号,[drugsBuyTotalPrice]总价 ,[drugsBuyerName]采购人员 ,[drugsBuyDate]采购日期 from drugsBuy order by drugsId";
                    dt2 = op.getDataTable(sql2, "drugsBuy");
                    dataGridView1.DataSource = dt2;
                    MessageBox.Show("删除成功！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.textBox1.Text = "";
                }


            }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message);
                  return;
              }    
          }
    }
}
